package Service;

import Bean.Page;
import Bean.Register;
import Bean.User;
import Dao.RegisterDao;
import Utils.UUIDUtils;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-09
 * Time: 13:25
 */
public class RegisterService {
    private RegisterDao registerDao = new RegisterDao();
    private UserService userService = new UserService();
    //1.新增注册申请
    public boolean register(User user) {
        Register register = new Register();
        register.setRegister_id(UUIDUtils.getId());
        register.user = user;
        return  registerDao.insert(register);
    }

    //2.管理员查看所有申请
    public Page select (int pageNo,int pageSize) {
        Page page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setTotalRecords(registerDao.size());
        page.setList(registerDao.select(pageNo,pageSize));
        return page;
    }

    //3.用户申请通过
    public boolean registerPass (String registerId) {
        //先通过registerId查询出对应的user数据
        User user = registerDao.selectUser(registerId);
        boolean ok = userService.insert(user);
        if(!ok) {
            return false;
        }
        //删除registerId这一条数据
        return registerDao.delete(registerId);
    }


    //4.用户请求不通过就直接删除这条请求 --- 只会在群删的时候使用到,参数就直接list了
    public boolean delete(String[] registerIds) {
        //直接foreach开删
        for (String registerId:registerIds) {
            boolean ok = registerDao.delete(registerId);
            if(!ok) {
                return false;
            }
        }
        return true;
    }

    //5.批量通过
    //3.用户申请通过
    public boolean registerPass (String[] registerIds) {
        for (String registerId:registerIds) {
            //先通过registerId查询出对应的user数据
            User user = registerDao.selectUser(registerId);
            boolean ok = userService.insert(user);
            if(!ok) {
                return false;
            }
            //删除registerId这一条数据
            ok = registerDao.delete(registerId);
            if(!ok) {
                return false;
            }
        }
        return true;
    }
}
